#include "ProxyServerNetWSModule.h"
#include "ProxyServerNetServerPlugin.h"
#include "ProxyServerNetServerModule.h"

bool FragrantGrass::ProxyServerNetWSModule::Init()
{
	//m_pWSModule = m_pPluginManager->FindModule<IWSModule>();
	m_pNetClientModule = m_pPluginManager->FindModule<INetClientModule>();
	m_pLogModule = m_pPluginManager->FindModule<ILogModule>();
	m_pProxyToWorldModule = m_pPluginManager->FindModule<IProxyServerToWorldModule>();
	m_pSecurityModule = m_pPluginManager->FindModule<ISecurityModule>();

    return true;
}

bool FragrantGrass::ProxyServerNetWSModule::AfterInit()
{
  //  FG_SHARE_PTR<NFIClass> xLogicClass = m_pClassModule->GetElement(NFrame::Server::ThisName());
  //  if (xLogicClass)
  //  {
		//const std::vector<std::string>& strIdList = xLogicClass->GetIDList();
		//for (int i = 0; i < strIdList.size(); ++i)
		//{
		//	const std::string& strId = strIdList[i];

  //          const int nServerType = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::Type());
  //          const int nServerID = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::ServerID());
  //          if (nServerType == FG_SERVER_TYPES::FG_ST_PROXY && m_pPluginManager->GetAppID() == nServerID)
  //          {
  //              const int nPort = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::Port());
  //              const int nMaxConnect = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::MaxOnline());
  //              const int nCpus = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::CpuCount());
  //              //const std::string& strName = m_pElementModule->GetPropertyString(strId, NFrame::Server::ID());
  //              //const std::string& strIP = m_pElementModule->GetPropertyString(strId, NFrame::Server::IP());
  //              int nWSPort = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::WSPort());

  //              //webserver only run one instance in each server
  //              if (nWSPort > 0)
  //              {
  //                  int nRet = m_pWSModule->Initialization(nMaxConnect, nWSPort, nCpus);
  //                  if (nRet < 0)
  //                  {
  //                      std::ostringstream strLog;
  //                      strLog << "Cannot init websocket server net, Port = " << nWSPort;
  //                      m_pLogModule->LogNormal(ILogModule::LL_ERROR_NORMAL, NULL_OBJECT, strLog, __FUNCTION__, __LINE__);
  //                      NFASSERT(nRet, "Cannot init websocket server net", __FILE__, __FUNCTION__);
  //                      exit(0);
  //                  }

	 //               m_pWSModule->AddEventCallBack(this, &FragrantGrass::ProxyServerNetWSModule::OnSocketClientEvent);
	 //               m_pWSModule->AddReceiveCallBack(this, &FragrantGrass::ProxyServerNetWSModule::OnWebSocketTestProcess);
  //                  break;
  //              }
  //              
  //          }
  //      }
  //  }

    return true;
}

bool FragrantGrass::ProxyServerNetWSModule::Shut()
{
    return true;
}

bool FragrantGrass::ProxyServerNetWSModule::Execute()
{
	return true;
}

void FragrantGrass::ProxyServerNetWSModule::OnWebSocketTestProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    //m_pWSModule->SendMsg(std::string(msg, nLen), nSockIndex);
}

void FragrantGrass::ProxyServerNetWSModule::OnSocketClientEvent(const SOCK nSockIndex, const FG_NET_EVENT eEvent, INet* pNet)
{
    if (eEvent & FG_NET_EVENT_EOF)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_EOF", "Connection closed", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else if (eEvent & FG_NET_EVENT_ERROR)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_ERROR", "Got an error on the connection", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else if (eEvent & FG_NET_EVENT_TIMEOUT)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_TIMEOUT", "read timeout", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else  if (eEvent & FG_NET_EVENT_CONNECTED)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_CONNECTED", "connected success", __FUNCTION__, __LINE__);
        OnClientConnected(nSockIndex);
    }
}

void FragrantGrass::ProxyServerNetWSModule::OnClientDisconnect(const SOCK nAddress)
{
    //NetObject* pNetObject = m_pWSModule->GetNet()->GetNetObject(nAddress);
    //if (pNetObject)
    //{
    //    int nGameID = pNetObject->GetGameID();
    //    if (nGameID > 0)
    //    {
    //        //when a net-object bind a account then tell that game-server
    //        if (!pNetObject->GetUserID().IsNull())
    //        {
    //            /*
    //            NFMsg::ReqLeaveGameServer xData;
				//xData.set_arg(nGameID);

    //            NFMsg::MsgBase xMsg;

				////real user id
    //            *xMsg.mutable_player_id() = NFINetModule::NFToPB(pNetObject->GetUserID());

    //            if (!xData.SerializeToString(xMsg.mutable_msg_data()))
    //            {
    //                return;
    //            }

    //            std::string strMsg;
    //            if (!xMsg.SerializeToString(&strMsg))
    //            {
    //                return;
    //            }

				//m_pNetClientModule->SendByServerIDWithOutHead(nGameID, NFMsg::EGameMsgID::EGMI_REQ_LEAVE_GAME, strMsg);
    //             */
  
    //        }
    //    }

    //    mxClientIdent.RemoveElement(pNetObject->GetClientID());
    //}
}

void FragrantGrass::ProxyServerNetWSModule::OnClientConnected(const SOCK nAddress)
{
	//bind client'id with socket id
    //GUID xClientIdent = m_pKernelModule->CreateGUID();
    //NetObject* pNetObject = m_pWSModule->GetNet()->GetNetObject(nAddress);
    //if (pNetObject)
    //{
    //    pNetObject->SetClientID(xClientIdent);
    //}

    //mxClientIdent.AddElement(xClientIdent, FG_SHARE_PTR<SOCK>(new SOCK(nAddress)));
}
